package com.nintendo.controller;

import com.nintendo.dto.EbookDTO;
import com.nintendo.entity.Ebook;
import com.nintendo.response.Result;
import com.nintendo.service.EbookService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/ebook")
@CrossOrigin
@Slf4j
public class EbookController {
    @Autowired
    private EbookService ebookService;
    @GetMapping
    public Result selectAllEbook(){
        Map<String, Object> map = new HashMap<>();
        map.put("data", ebookService.findAllEbooks());
        return Result.ok(map);
    }
    @PostMapping("/byNameAuthPage")
    public Result selectAllEbookByName(@RequestBody EbookDTO ebookDTO){
        Map<String, Object> map = new HashMap<>();
        map.put("data", ebookService.findAllEbooksByName(ebookDTO));
        return Result.ok(map);
    }
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id")Long id){
        ebookService.deleteById(id);
        return Result.ok("删除成功");
    }
    @GetMapping("/{id}")
    public Result selectEbookById(@PathVariable("id")Long id){

        return Result.ok().put("data",ebookService.findEbookById(id));
    }
    @PutMapping("/updateById")
    public Result updateEbookById(@RequestBody Ebook ebook){
        ebookService.updateEbookById(ebook);
        return Result.ok("修改成功");
    }
    @PostMapping("/saveEbook")
    public Result saveEbook(@Valid @RequestBody Ebook ebook){
        ebookService.saveEbook(ebook);
        return Result.ok("保存成功");
    }
    @GetMapping("/selectAllEbook")
    public Result selectAll(){
        return Result.ok().put("data",ebookService.getEbookCascaderVo());
    }
    @GetMapping("/byCategoryId2/{id}")
    public Result findByCategoryId2(@PathVariable("id")Long id){

        return Result.ok().put("data",ebookService.findByCategoryId2(id));
    }
}
